
#include "abcAll.h"
#include "customException.h"
#include <iostream>
#include <algorithm>

using namespace std;

static int illegalParameterValueExcepTest(void)
{
    try 
    {
        cout << abc(2, 0, 4) << endl;
    }
    catch(illegalParameterValue e)
    {
        cout << "The parameters to abc were 2, 0, and 4" << endl;
        cout << "illegalParameterValue exception thrown " << endl;
        e.outputMessage();
        return 1;
    }
    return 0;
}

static int rSumTest(void)
{
    int a[6] = {1, 2, 3, 4, 5, 6};

    cout << "a[0:5] = ";
    copy(a, a+6, ostream_iterator<int>(cout, " "));
    cout << endl;
    
#if 1

    cout << "rSum(a,0) = " << rSum(a,0) << endl;
    cout << "rSum(a,1) = " << rSum(a,1) << endl;
    cout << "rSum(a,2) = " << rSum(a,2) << endl;
    cout << "rSum(a,5) = " << rSum(a,5) << endl;
    cout << "rSum(a,6) = " << rSum(a,6) << endl;
    //cout << "rSum(a,6) = " << rSum(a,7) << endl;
#else
    int r = rSum(a, 2);
    cout << r << endl;
#endif
    return 0;
}



#include <stdint.h>

struct rept_itm {
    union {
        uint8_t byte;
        uint8_t* byte_seq;
    };
    int seq_sz;
    int rept_cnt;

};

uint32_t inline tri_bytes_to(uint8_t bytes[3])
{
    uint32_t r = 0;
    for (int i = 0; i < 3; i++) {
        r |= (uint32_t)bytes[i] << i * 8;
    }

    return r;
}
/*
 *  
 */
int search_b1_from(uint8_t* data, uint8_t target)
{
    uint8_t* st = data;

    while (*st == target) {
        st++;
    }

    return int(st - data);
}

int search_b2_from(uint8_t* data, uint8_t target[2])
{
    uint16_t* t = (uint16_t*)target;
    uint16_t* st = (uint16_t*)data;

    while (*st == *t) {
        st++;
    }

    return int(st - (uint16_t*)data);
}

int search_b3_from(uint8_t* data, uint8_t target[3])
{
    uint32_t t = tri_bytes_to(target);

    uint8_t* st = data;

    uint32_t d = tri_bytes_to(st);

    while (d == t) {
        st += 3;
        d = tri_bytes_to(st);
    }

    return int(st - data)/3;                                                                   
}

int search_b4_from(uint8_t* data, uint8_t target[4])
{
    uint32_t* t = (uint32_t*)target;
    uint32_t* st = (uint32_t*)data;

    while (*st == *t) {
        st++;
    }

    return int(st - (uint32_t*)data);
}

int search_bn_from(uint8_t* data, uint8_t target[], int target_sz)
{

    return 1;
}
/*
 * return the number of iteraed bytes in data
 */
int form_sequence_by_probe(uint8_t* data, int dt_sz, uint8_t* seq, int seq_sz)
{

}

int match_img_data(uint8_t* img_data, int sz)
{

    uint8_t* ptr = img_data;

    while (ptr < img_data + sz) {

    }

    
    return 0;
}



int search_bx_test(void)
{
    int n;
    
    uint8_t data[] = {
        0x00, 0x00, 0x00, 0x31, 0x31, 0x31, 0x31, 0x78, 0x32, 0x32, 
        0xc1, 0xc1, 0xc1, 0xc3

    };

    uint8_t t1[] = { 0x32, 0x32 };
    uint8_t t2[] = { 0xc1, 0xc1, 0xc1 };
    uint8_t t3[] = { 0x31, 0x31, 0x31, 0x31 };


    n = search_b1_from(data, 0x00);
    printf("n:%d\r\n", n);

    n = search_b2_from(&data[8], t1);
    printf("n:%d\r\n", n);

    n = search_b3_from(&data[10], t2);
    printf("n:%d\r\n", n);

    n = search_b4_from(&data[3], t3);
    printf("n:%d\r\n", n);

    return 0;

}

void abcAllTest(void)
{
    //illegalParameterValueExcepTest();
    //rSumTest();
    search_bx_test();
}